# -*- coding: utf-8 -*-

from odoo import api, models, fields
import odoo.addons.decimal_precision as dp

# 拓展 product.template
class ProductTemplate(models.Model):
    _inherit = 'product.template'

    is_financial = fields.Boolean("金融产品",default = True)
    # 具体的销售精度，可以在设置/技术/数据库结构/小数精度，通过设置 Product of Financial 进行调整
    annual_interest = fields.Float(string='年利率',digits=dp.get_precision('Product of Financial'),default=0.0000)
    sale_bonus = fields.Float(string='产品提成',digits=dp.get_precision('Product of Financial'),default=0.0000)

    type_financial= fields.Selection([
        ('1', '借款'),
        ('2', '基金'),
        ('3', '其他'),

    ], string='金融产品类型', default='1')

    @api.onchange('is_financial')
    def is_financial_change(self):
        if self.is_financial:
            self.type = "service"
        else :
             self.type = ""

#拓展 crm.team
class CrmTeam(models.Model):
    _inherit = 'crm.team'

    team_bonus = fields.Float(string='团队提成',digits=dp.get_precision('Product of Financial'), default=0.0000)

#拓展 sale.order
class SaleOrder(models.Model):
    _inherit = 'sale.order'

    @api.depends('order_line.price_total')
    def _sale_bonus(self):
        for order in self:
            product_bouns = team_bonus = 0.0
            for line in order.order_line:
                # product_bouns += line.price_subtotal * line.product_id.sale_bonus

                product_bouns += line.price_subtotal *  float(self.annual_interest )  #计算  价格小计  * 年利率

                # team_bonus += line.price_subtotal * self.team_id.team_bonus

            order.update({
                # 'sale_bonus': product_bouns + team_bonus,
                'sale_bonus': product_bouns ,
            })

    sale_bonus = fields.Float(string='业绩提成',default=0.00, store=True, readonly=True, compute='_sale_bonus')


    subscription_period = fields.Selection([
    ('3', '3个月'),
    ('6', '6个月'),
    ('12', '12个月'),
    ('24', '24个月'),
    ('36', '36个月'),
    ('48', '48个月'),
    ('60', '60个月'),
    ], string='认购期限',default = '12')


    # annual_interest = fields.Float(string='年利率', digits=dp.get_precision('Product of Financial'), default=1.0000)
    # sale_bonus = fields.Float(string='产品提成', digits=dp.get_precision('Product of Financial'), default=0.0000)

    annual_interest = fields.Selection([
        ('0.075', '%7.5'),('0.08', '%8'),('0.085', '%8.5'),('0.09', '%9'),('0.095', '%9.5'),('0.10', '%10'),
        ('0.105', '%10.5'),('0.11', '%11'),('0.115', '%11.5'),('0.12', '%12'),('0.125', '%12.5'),('0.13', '%13'),
        ('0.135', '%13.5'), ('0.14', '%14'), ('0.145', '%14.5'), ('0.15', '%15'), ('0.155', '%15.5'), ('0.16', '%16')
    ], string='年利率', default="0.075")

    repayment_status = fields.Boolean("还款状态", default=False)

    hetongbianhao = fields.Char(u'合同编号', help=u"合同编号")
    kaihuhang = fields.Char(u'开户行', help=u"开户行")
    yinhangzhanghao = fields.Char(u'银行账号', help=u"银行账号")

#拓展 'res.partner'
class ResPartner(models.Model):
    _inherit = 'res.partner'
    rescard=fields.Char(u'身份证',  help=u"身份证")

    user_id = fields.Many2one('res.users',
                              string='销售员',
                              default=lambda self: self.env.uid,
                              index=True, track_visibility='always')




#拓展 'res.partner'
class ResPartner(models.Model):
    _inherit = 'res.partner'
    rescard=fields.Char(u'身份证',  help=u"身份证" ,  required=True)

    user_id = fields.Many2one('res.users',
                              string='销售员',
                              default=lambda self: self.env.uid,
                              index=True, track_visibility='always')

    mobile = fields.Char(help="亲，客户的手机要填哦！" ,  required=True)

    # @api.one
    # @api.depends('mobile')
    @api.constrains('mobile')
    def _sale_bonus(self):
        shouji=self.search([('mobile', '=', self.mobile)])
        print len(shouji), 'oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo'
        if len(shouji)>1:
            raise ValueError(('已经存在相同的用户,请联系管理员'))



#拓展 '	account.invoice'
class ResPartner(models.Model):
    _inherit = 'account.invoice'

    # repayment_status = fields.Boolean("还款状态", default=False)